<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>正则表达式</title>
</head>
<body>
    
    <script>
    // 1 - 判断字符串是否包含数字

    function hasNumber (str) {
        var reg = /\d+/g;
        return reg.test(str);
    }

    console.log(hasNumber("sdfsdf123gfdgdf134345423"));

    // 2 - 判断连续重复字母

    function consitantLetter (str) {
        var reg = /[a-zA-z]/g;
        return reg.test(str);
    }

    console.log(consitantLetter ("DHJHDKJHKJKJhijkl"));

    // 3 - 

    function fn (str) {
        return str.replace(/(\d)+/g,function () {
            console.log(arguments);
        });
    }

    console.log(fn("abcd1234fghjd5678"));

    // 4 - "https://www.baidu.com/s?wd=js&rsv=1&qid=test&issp=0&f=8&bp=1001"

    function urlToObj (str) {
        var reg = /([^?&]+)=([^?&]+)/g;
        var obj = {};
        str.replace(reg,function () {
            obj[arguments[1]] = arguments[2];
            console.log(`arguments -- ${arguments.toString()}`);
        })
        return obj;
    }

    console.log(urlToObj ("https://www.baidu.com/s?wd=js&rsv=1&qid=test&issp=0&f=8&bp=1001"));
    

    // 5 - "get-element-by-id"; -> getElementById

    function g (str) {
        var arr = str.split("-");
        var ary = [];
        for(var i=1;i<arr.length;i++){
            ary.push(arr[i][0].toUpperCase() + arr[i].slice(1));  
        }
        ary.unshift(arr[0]);
        return ary.join("");
    }

    console.log(g("get-element-by-id"));

    // 判断是否符合 USD 格式

    function checkUSD (str) {
        return /^\$[1-9]\d{0,2}(,\d{3})*(\.\d{2})?$/.test(str);
    }
    console.log(checkUSD("$200,000.98"));

    // YYYY-MM-DD

    function timeFormat (str) {
        return /^[1-9]\d{3}(-\d{2}){2}$/.test(str);
    }

    console.log(timeFormat("1818-09-09"));

    // 输入内容是否为英文字母

    function checkNull (str) {
        return /^[a-zA-Z]+$/.test(str);
    }

    console.log(checkNull("sdgdhfjbjfiow327983928764sgdsghdsdf"));


    //判断输入的字符是否为中文

    function isChinese (str) {
        return /^[\u0391-\uFFE5]+$/.test(str);
    }

    console.log(`isChinese -- ${isChinese ("this is a cat")}`);
    console.log(`isChinese -- ${isChinese ("这是一只猫")}`);


    // 判断是否为整数

    function isInteger (num) {
        return num%1 === 0;
    }

    function regInteger (n) {
        return /^-?\d+$/g.test(n);
    }

    function integer (n) {
        return Number.isInteger(n);
    }

    console.log(`isInteger -- ${isInteger (20)}`);
    console.log(`isInteger -- ${isInteger (20.23)}`);
    console.log(`regInteger -- ${regInteger (20.23)}`);
    console.log(`regInteger -- ${regInteger (88)}`);
    console.log(`regInteger -- ${regInteger (-120)}`);

    // 18-65 

    function numRange (s) {
        return /^(1[0-8]|[2-5]\d|6[0-5])$/.test(s);
    }
    
    console.log(`numRange -- ${numRange(895423)}`);
    console.log(`numRange -- ${numRange(33)}`);

    //判断输入的EMAIL格式是否正确   

    function isEmail (str) {
        return /^(\w)+@(\w)+\.(com|cn|net)$/.test(str);
    }
   
    console.log("email -- " + isEmail("shuowang1991@163.com"));
    console.log("email -- " + isEmail("1556260806@qq.com"));
    console.log("email -- " + isEmail("sdfjids@com"));

    //

    
    </script>
</body>
</html>